package com.ruoyi.common.service;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.ruoyi.common.core.service.OssService;
import com.ruoyi.common.domain.bo.DashboardBo;
import com.ruoyi.common.domain.vo.*;
import com.ruoyi.standingbook.extermination.domain.bo.ExterminationBo;
import com.ruoyi.standingbook.extermination.domain.vo.ExterminationVo;
import com.ruoyi.standingbook.extermination.mapper.ExterminationMapper;
import com.ruoyi.standingbook.extermination.service.IExterminationService;
import com.ruoyi.standingbook.recycle.domain.bo.RecycleBo;
import com.ruoyi.standingbook.recycle.domain.vo.RecycleVo;
import com.ruoyi.standingbook.recycle.mapper.RecycleMapper;
import com.ruoyi.standingbook.recycle.service.IRecycleService;
import com.ruoyi.standingbook.sorting.domain.bo.SortingBo;
import com.ruoyi.standingbook.sorting.domain.vo.SortingVo;
import com.ruoyi.standingbook.sorting.mapper.SortingMapper;
import com.ruoyi.standingbook.sorting.service.ISortingService;
import com.ruoyi.standingbook.toilet.domain.bo.ToiletBo;
import com.ruoyi.standingbook.toilet.domain.vo.ToiletVo;
import com.ruoyi.standingbook.toilet.mapper.ToiletMapper;
import com.ruoyi.standingbook.toilet.service.IToiletService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@RequiredArgsConstructor
@Service
public class DashboardService {
    private final IRecycleService recycleService;
    private final RecycleMapper recycleMapper;
    private final IExterminationService exterminationService;
    private final ExterminationMapper exterminationMapper;
    private final IToiletService toiletService;
    private final ToiletMapper toiletMapper;
    private final ISortingService sortingService;
    private final SortingMapper sortingMapper;
    private final OssService ossService;


    /**
     * 获取仪表盘数据信息
     *
     * @param bo
     * @return
     */
    public DashboardVo getDashboardInfo(DashboardBo bo) {
        //今日清运桶数
        Long todayBarrelage = recycleMapper.statisticsTodayBarrelage();
        //累计清运千桶数
        Double totalBarrelage = recycleMapper.statisticsTotalBarrelage();

        //今日消杀次数
        Long todayFrequency = exterminationMapper.statisticsTodayFrequency();
        //累计消杀次数
        Double totalFrequency = exterminationMapper.statisticsTotalFrequency();

        //统计公厕管养完成情况数量
        List<StatisticsToiletVo> toiletStatusNumList = toiletMapper.statisticsStatusNum();
        Double toiletNum = 0d; //公厕管养总数量
        Double toiletCompleteNum = 0d; //公厕管养完成数量
        Double totalToiletCompletionRatio = 0d; //公厕管养完成比例
        for (StatisticsToiletVo stv : toiletStatusNumList) {
            toiletNum += stv.getNum();
            if (stv.getStatus().equals("已完成")) {
                toiletCompleteNum += stv.getNum();
            }
        }
        totalToiletCompletionRatio = toiletCompleteNum != 0 ? (toiletCompleteNum / toiletNum) * 100 : 0;


        Double totalSortingTodayCompletionRatio = this.getTotalSortingTodayCompletionRatio(); //合计今日督导完成比例

        DashboardVo dv = new DashboardVo();
        dv.setRecycleTodayBarrelage(todayBarrelage);
        dv.setRecycleTotalBarrelage(totalBarrelage);
        dv.setTodayFrequency(todayFrequency);
        dv.setTotalFrequency(new BigDecimal(totalFrequency.toString()).toString());
        dv.setTotalToiletCompletionRatio(totalToiletCompletionRatio.intValue());
        dv.setTotalSortingTodayCompletionRatio(totalSortingTodayCompletionRatio.intValue());
        return dv;
    }

    /**
     * 获取垃圾清运电子档案信息
     *
     * @param bo
     * @return
     */
    public RecycleErecordVo getRecycleErecordInfo(RecycleBo bo) {
        return recycleService.getRecycleErecordInfo(bo);
    }

    /**
     * 获取垃圾清运工作日志
     *
     * @param bo
     * @return
     */
    public RecycleLogVo getRecycleLog(RecycleBo bo) {
        if (StringUtils.isBlank(bo.getDate())) {
            throw new RuntimeException("日期不能为空");
        }
        if (StringUtils.isBlank(bo.getGarbageCategory())) {
            throw new RuntimeException("垃圾类别不能为空");
        }
        RecycleLogVo rlv = new RecycleLogVo();
        List<RecycleVo> rvList = recycleMapper.recycleList(bo);
        List<String> picList1 = new ArrayList<>();
        List<String> picList2 = new ArrayList<>();
        List<String> picList3 = new ArrayList<>();
        for (RecycleVo rv : rvList) {
            picList1.add(rv.getPic1() != null ? ossService.selectUrlByIds(rv.getPic1()) : "");
            //picList1.add(rv.getPic2() != null ? ossService.selectUrlByIds(rv.getPic2()) : "");

            picList2.add(rv.getPic3() != null ? ossService.selectUrlByIds(rv.getPic3()) : "");
            //picList2.add(rv.getPic4() != null ? ossService.selectUrlByIds(rv.getPic4()) : "");

            picList3.add(rv.getPic5() != null ? ossService.selectUrlByIds(rv.getPic5()) : "");
            //picList3.add(rv.getPic6() != null ? ossService.selectUrlByIds(rv.getPic6()) : "");
        }
        rlv.setRecycleList(rvList); //垃圾清运列表
        rlv.setPicList1(picList1);  //出车前检查图片列表
        rlv.setPicList2(picList2);  //清运过程图片列表
        rlv.setPicList3(picList3);  //车辆进站图片列表
        return rlv;
    }

    /**
     * 获取四害消杀电子档案信息
     *
     * @param bo
     * @return
     */
    public ExterminationErecordVo getExterminationErecordInfo(ExterminationBo bo) {
      return exterminationService.getExterminationErecordInfo(bo);
    }

    /**
     * 获取四害消杀工作日志
     *
     * @param bo
     * @return
     */
    public ExterminationLogVo getExterminationLog(ExterminationBo bo) {
        if (StringUtils.isBlank(bo.getDate())) {
            throw new RuntimeException("日期不能为空");
        }
        if (StringUtils.isBlank(bo.getExterminationCategory())) {
            throw new RuntimeException("消杀类别不能为空");
        }
        ExterminationLogVo elv = new ExterminationLogVo();
        List<ExterminationVo> rvList = exterminationMapper.exterminationList(bo);
        List<String> picList1 = new ArrayList<>();
        List<String> picList2 = new ArrayList<>();
        List<String> picList3 = new ArrayList<>();
        List<String> picList4 = new ArrayList<>();
        for (ExterminationVo ev : rvList) {
            picList1.add(ev.getPic1() != null ? ossService.selectUrlByIds(ev.getPic1()) : "");
            //picList1.add(ev.getPic2() != null ? ossService.selectUrlByIds(ev.getPic2()) : "");

            picList2.add(ev.getPic3() != null ? ossService.selectUrlByIds(ev.getPic3()) : "");
           // picList2.add(ev.getPic4() != null ? ossService.selectUrlByIds(ev.getPic4()) : "");

            picList3.add(ev.getPic5() != null ? ossService.selectUrlByIds(ev.getPic5()) : "");
            //picList3.add(ev.getPic6() != null ? ossService.selectUrlByIds(ev.getPic6()) : "");

            picList4.add(ev.getPic7() != null ? ossService.selectUrlByIds(ev.getPic7()) : "");
            //picList4.add(ev.getPic8() != null ? ossService.selectUrlByIds(ev.getPic8()) : "");
        }
        elv.setExterminationList(rvList); //四害消杀列表
        elv.setPicList1(picList1);  //药品图片列表
        elv.setPicList2(picList2);  //现场完成前图片列表
        elv.setPicList3(picList3);  //现场过程图片列表
        elv.setPicList4(picList4);  //现场完成后图片列表
        return elv;
    }

    /**
     * 获取市政公厕管养电子档案信息
     *
     * @param bo
     * @return
     */
    public ToiletErecordVo getToiletErecordInfo(ToiletBo bo) {
        return toiletService.getToiletErecordInfo(bo);
    }

    /**
     * 获取市政公厕管养工作日志
     *
     * @param bo
     * @return
     */
    public ToiletLogVo getToiletLog(ToiletBo bo) {
        if (StringUtils.isBlank(bo.getDate())) {
            throw new RuntimeException("日期不能为空");
        }
        if (StringUtils.isBlank(bo.getCommunity())) {
            throw new RuntimeException("社区不能为空");
        }
        ToiletLogVo tlv = new ToiletLogVo();
        List<ToiletVo> tvList = toiletMapper.toiletList(bo);
        List<String> picList1 = new ArrayList<>();
        List<String> picList2 = new ArrayList<>();
        List<String> picList3 = new ArrayList<>();
        for (ToiletVo tv : tvList) {
            picList1.add(tv.getPic1() != null ? ossService.selectUrlByIds(tv.getPic1()) : "");
            //picList1.add(tv.getPic2() != null ? ossService.selectUrlByIds(tv.getPic2()) : "");

            picList2.add(tv.getPic3() != null ? ossService.selectUrlByIds(tv.getPic3()) : "");
            //picList2.add(tv.getPic4() != null ? ossService.selectUrlByIds(tv.getPic4()) : "");

            picList3.add(tv.getPic5() != null ? ossService.selectUrlByIds(tv.getPic5()) : "");
            //picList3.add(tv.getPic6() != null ? ossService.selectUrlByIds(tv.getPic6()) : "");
        }
        tlv.setToiletList(tvList); //市政公厕管养列表
        tlv.setPicList1(picList1);  //现场清洁前图片列表
        tlv.setPicList2(picList2);  //现场清洁中图片图片列表
        tlv.setPicList3(picList3);  //现场清洁后图片列表
        return tlv;
    }

    /**
     * 获取垃圾分类督导电子档案信息
     *
     * @param bo
     * @return
     */
    public SortingErecordVo getSortingErecordInfo(SortingBo bo) {
        return sortingService.getSortingErecordInfo(bo);
    }

    /**
     * 得到今日督导完成比例
     *
     * @return
     */
    private Double getTotalSortingTodayCompletionRatio() {
        Double totalTodayNum = 0d;  //统计所有今日总数量
        Double completionToadyNum = 0d; //统计所有今日完成督导的数量
        Double totalTodayCompletionRatio = 0d; //合计今日督导完成比例
        List<StatisticsSortingVo> sortingTodayList = sortingMapper.statisticsTodayDurationList(); //统计今天督导时长分组列表
        for (StatisticsSortingVo ssv : sortingTodayList) {
            SortingBo bo = new SortingBo();
            bo.setCommunity(ssv.getCommunity());

            Double totalNum = 0d;  //总数量
            Double completionNum = 0d; //完成督导的数量
            Double completionRatio = 0d;//督导完成比例
            List<StatisticsSortingVo> todayCompletionList = sortingMapper.statisticsTodayCompletionList(bo);         //统计今天完成督导的数量
            List<StatisticsSortingVo> todayNotCompletionList = sortingMapper.statisticsTodayNotCompletionList(bo);  //统计今天未完成督导的数量
            if (CollectionUtils.isNotEmpty(todayCompletionList)) {
                completionNum = todayCompletionList.get(0).getCompletionNum();
                totalNum += completionNum;
                completionToadyNum += completionNum;
                totalTodayNum += completionNum;
            }
            if (CollectionUtils.isNotEmpty(todayNotCompletionList)) {
                Double notCompletionNum = todayNotCompletionList.get(0).getNotCompletionNum();
                totalNum += notCompletionNum;
                totalTodayNum += notCompletionNum;
            }
            completionRatio = (completionNum / totalNum) * 100;  //督导完成比例
            ssv.setCompletionRatio(completionRatio);
        }
        totalTodayCompletionRatio = completionToadyNum != 0 ? (completionToadyNum / totalTodayNum) * 100 : 0;  //合计今日督导完成比例
        return totalTodayCompletionRatio;
    }

    /**
     * 获取市政公厕管养工作日志
     *
     * @param bo
     * @return
     */
    public SortingLogVo getSortingLog(SortingBo bo) {
        if (StringUtils.isBlank(bo.getDate())) {
            throw new RuntimeException("日期不能为空");
        }
        if (StringUtils.isBlank(bo.getCommunity())) {
            throw new RuntimeException("社区不能为空");
        }
        SortingLogVo slv = new SortingLogVo();
        List<SortingVo> svList = sortingMapper.sortingList(bo);
        List<String> picList1 = new ArrayList<>();
        List<String> picList2 = new ArrayList<>();
        List<String> picList3 = new ArrayList<>();
        for (SortingVo tv : svList) {
            picList1.add(tv.getPic1() != null ? ossService.selectUrlByIds(tv.getPic1()) : "");
            //picList1.add(tv.getPic2() != null ? ossService.selectUrlByIds(tv.getPic2()) : "");

            picList2.add(tv.getPic3() != null ? ossService.selectUrlByIds(tv.getPic3()) : "");
            //picList2.add(tv.getPic4() != null ? ossService.selectUrlByIds(tv.getPic4()) : "");

            picList3.add(tv.getPic5() != null ? ossService.selectUrlByIds(tv.getPic5()) : "");
            //picList3.add(tv.getPic6() != null ? ossService.selectUrlByIds(tv.getPic6()) : "");
        }
        slv.setSortingList(svList); //垃圾分类督导列表
        slv.setPicList1(picList1);  //岗前培训图片列表
        slv.setPicList2(picList2);  //现场督导图片列表
        slv.setPicList3(picList3);  //厨余垃圾情况图片列表
        return slv;
    }

}
